
package com.cn.springcloudwechat.sms;

import com.alibaba.fastjson.JSONException;
import com.cn.springcloudwechat.model.sms.Sms;
import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;

import javax.xml.ws.http.HTTPException;

import java.io.IOException;

import static com.cn.springcloudwechat.model.sms.Sms.appid;
import static com.cn.springcloudwechat.model.sms.Sms.appkey;
import static com.cn.springcloudwechat.model.sms.Sms.templateId;

public class SmsSendUtils {


    /**
     * 单发短信
     * @param phone
     * @param random
     * @return
     */
    public static String sendOne(String phone,String random){
        final SmsSingleSenderResult[] result = {null};

                try {
                    SmsSingleSender ssender = new SmsSingleSender(Sms.appid, Sms.appkey);
                    result[0] = ssender.send(0, "86", phone,
                            random+" 为你的验证码,若为本人填写,请忽略该消息", "", "");
                    System.out.print(result[0]);
                } catch (HTTPException e) {
                    // HTTP响应码错误
                    e.printStackTrace();
                } catch (JSONException e) {
                    // json解析错误
                    e.printStackTrace();
                } catch (IOException e) {
                    // 网络IO错误
                    e.printStackTrace();
                } catch (com.github.qcloudsms.httpclient.HTTPException e) {
                    e.printStackTrace();
                }

        return result[0].errMsg;
    }

    /**
     * 模板消息
     * @param phone
     * @param random
     */
    public static String sendTempates(String phone,String[] random){
        SmsSingleSenderResult result=null;
        try {
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            result = ssender.sendWithParam("86", phone,
                    templateId, random, "", "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            System.out.print(result);
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        } catch (com.github.qcloudsms.httpclient.HTTPException e) {
            e.printStackTrace();
        }
        return result.errMsg;
    }

}

